package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.entity.CheckItem;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangmeng
 * @description 检查组Controller
 * @date 2019/9/16
 **/

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class checkGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     *
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        log.info("新增检查组:{}，{}", checkGroup, checkitemIds);
        checkGroupService.add(checkGroup, checkitemIds);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询检查组
     *
     * @param queryPageBeanDTO
     * @return
     */
    @GetMapping("/findPage")
    public Result pageQuery(QueryPageBeanDTO queryPageBeanDTO) {
        log.info("分页查询检查组:{}", queryPageBeanDTO);
        PageResult pageResult = checkGroupService.pageQuery(queryPageBeanDTO);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("根据id查询检查组:{}", id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, checkGroup);
    }

    /**
     * 根据组id查询包含检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("根据组id查询包含检查项:{}", id);
        List<Integer> checkItems = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, checkItems);
    }

    /**
     * 修改检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        log.info("修改检查组:{},{}", checkGroup, checkitemIds);
        checkGroupService.edit(checkGroup, checkitemIds);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id删除检查组
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Integer id) {
        log.info("根据id删除检查组：{}", id);
        checkGroupService.deleteCheckGroupitemById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 查询全部检查组信息
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        log.info("查询全部检查组信息");
        List<CheckGroup> checkGroups = checkGroupService.findALL();
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroups);
    }
}
